/*
#include <stdlib.h>
void exit(int status);
int atexit(void (*function)(void));
int on_exit(void (*function)(int, void *), void *arg);

#include <unistd.h>
void _exit(int status);

#include <assert.h>
void assert(int expression);

*/

/*
	int atexit(void (*function)(void));
*/

#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "error.h"

void forkerror(void);
void execerror(void);

int main(int argc, char * argv[]) {
	pid_t pid;

	if((pid = fork()) < 0) {
		atexit(forkerror);
	} else if(pid == 0) {
		fprintf(stdout, "Child process PID: %d.\n", getpid());
		setenv("PS1", "CHILD\\$", 1);
		fprintf(stdout, "Process %d: calling exec.\n", getpid());

		if(execlp("/bin/bash", "/bin/bash", "arg2", NULL) < 0) {
			atexit(execerror);
		}
		fprintf(stdout, "Process %d: You should never see this because the child is already gnoe.\n", getpid());
		fprintf(stdout, "Process %d: The child process is exiting...\n", getpid());
	}

	return 0;;
}

void forkerror(void) {
	fprintf(stdout, "%s\n", "Fork error.");
	exit(1);
}

void execerror(void) {
	fprintf(stdout, "%s\n", "Exec error.");
	exit(1);
}
